<template>
  <div id="home">
    <!-- <h2>This is Home</h2> -->
    <!-- <div class="home_box"></div> -->
    <!-- 主体内容 -->
    <el-container class="home_container">
      <!-- herder -->
      <el-header>
        <div>
          <img src="../assets/img/avatorDefault.jpg" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="loginout">退出</el-button>
      </el-header>
      <el-container>
        <!-- aside -->
        <!-- 动态绑定宽度 -->
        <el-aside :width="isCollapse ? '64px':'200px'">
          <!-- 侧边栏区 -->
          <!-- 折叠、展开菜单文本 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- unique-opened 只保持一个子菜单的展开  写法：:unique-opened="true" 或 unique-opened -->
          <!-- router : 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
          <el-menu unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath" background-color="#333" text-color="#fff" active-text-color="#409EFF">
            <!-- 一级菜单 -->
            <!-- index 接收字符串类型，用于显示不同的菜单项 -->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单标题 -->
              <template slot="title">
                <!-- icon图标 -->
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <!-- index 用于显示不同的菜单项，每个菜单项的path值唯一 -->
              <!-- menu 启用 vue-router 的模式，此处将 index 改为 path路径 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- main -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存二级菜单链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  //
  #home,.home_container{
    width: 100%;height: 100%;
  }
  .el-header,.el-aside{
    background-color: #333;
  }
  .el-main{
    background-color: lavender;
  }
  .el-aside .el-menu{
    border-right: none;
  }
  //
  .el-header{
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-header img{
    width: 50px;
    border-radius: 50%;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
